﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
#include"SeqList.h"
void ContactInit(contact* pcon) {
	SLInit(pcon);
}
void ContactDestory(contact* pcon) {
	SLDestory(pcon);
}
void ContactAdd(contact* pcon) {
	CInfo info;
	printf("请输入联系人姓名\n");
	scanf("%s", info.name);
	printf("请输入联系人性别\n");
	scanf("%s", info.sex);
	printf("请输入联系人年龄\n");
	scanf("%d", &info.age);
	printf("请输入联系人电话\n");
	scanf("%s", info.tel);
	printf("请输入联系人地址\n");
	scanf("%s", info.addr);
	//往顺序表中插入目前存放在结构体的数据
	SLPushBack(pcon, info);
}
int FindByName(contact* pcon, char name[])
{
	for (int i = 0; i < pcon->size; i++)
	{
		if (strcmp(pcon->a->name, name) == 0)
		{
			return i;
		}
	}
	return -1;
}

void ContactDel(contact* pcon) {
	//强制要求用户使用姓名查找用户
	printf("请输入你要删除的用户名称\n");
	char name[NAME_MAX];
	scanf("%s", name);
	int finfidex = FindByName(pcon, name);
	if (finfidex < 0)
	{
		printf("没有该联系人\n");
		return;
	}
	SLErase(pcon, finfidex);
}
void ContactShow(contact* pcon) {
	printf("%-10s %-4s %-4s %15s %-20s\n", "姓名", "性别", "年龄", "联系电话", "地址");
		for (int i = 0; i < pcon->size; i++)
		{
			printf("%-10s %-4s %-4d %15s %-20s\n",
				pcon->a[i].name,
				pcon->a[i].sex,
				pcon->a[i].age,
				pcon->a[i].tel,
				pcon->a[i].addr);
		}
}
void ContactModify(contact* pcon) {
	char name[NAME_MAX];
	printf("请输入要修改的用户名称:\n");
	scanf("%s", name);
	int pos = FindByName(pcon, name);
	if (pos < 0) {
		printf("要查找的用户不存在，修改失败！\n");
		return;
	}
	printf("请输入要修改的姓名：\n");
	scanf("%s", &pcon->a[pos].name);
	printf("请输入要修改的性别：\n");
	scanf("%s", &pcon->a[pos].sex);
	printf("请输入要修改的年龄：\n");
	scanf("%d", &pcon->a[pos].age);
	printf("请输入要修改的联系电话：\n");
	scanf("%s", &pcon->a[pos].tel);
	printf("请输入要修改的地址：\n");
	scanf("%s", &pcon->a[pos].addr);
	printf("修改成功！\n");
}
void ContactFind(contact* pcon){

char name[NAME_MAX];
printf("请输入要查找的用户姓名：\n");
scanf("%s", name);
int pos = FindByName(pcon, name);
if (pos < 0) {
	printf("要查找的用户不存在，查找失败！\n");
	return;
}
printf("查找成功！\n");
printf("%-10s %-4s %-4d %15s %-20s\n",
	pcon->a[pos].name,
	pcon->a[pos].sex,
	pcon->a[pos].age,
	pcon->a[pos].tel,
	pcon->a[pos].addr);
}